// .........................................................................
// Title: Morningstar_Build.do
//
// Overall context manager for Stata Morningstar build jobs
// .........................................................................

* ---------------------------------------
* Set up environment
* ---------------------------------------

* main globals
global whoami <WHOAMI>
global code_path <CODE_PATH>
global project_path <PROJECT_PATH>

* paths
global raw "$project_path/raw"
global logs "$project_path/logs"
global tmp "$project_path/temp"
global temp "$project_path/temp"
global graphs "$project_path/graphs"
global output "$project_path/output"
global tables "$project_path/tables"
global holdings "$project_path/holdings"
global morningstar_hd "$output/morningstar_holding_detail"
global insurance_holdings "$project_path/insurance_master"
global dir_mstar_raw "$raw/morningstar"
global reconstruct "$temp/reconstruct"
global cgstemp "$temp/cgs"

* log file
cap mkdir "$logs/`2'"
cap log close 
if "`5'"!="" {
    log using "$logs/`2'/${whoami}_`2'_Array_`5'.log", replace
}
else {
    log using "$logs/`2'/${whoami}_`2'.log", replace
}

* set year variables from arguments 
global firstyear = `3'
global lastyear = `4'
global lastyear_mf = `4'
di "firstyear = `3'"
di "lastyear = `4'"
di "lastyear_mf = `4'"

* make general folders
cap mkdir $output
cap mkdir $temp
cap mkdir $logs
cap mkdir $graphs
cap mkdir $tables
cap mkdir $holdings
cap mkdir $morningstar_hd
cap mkdir $insurance_holdings
cap mkdir $cgstemp
cap mkdir $reconstruct
cap mkdir $output/morningstar_mapping
cap mkdir $output/hd_panels
cap mkdir $temp/ER_data
cap mkdir $reconstruct/intermediate
cap mkdir $output/Internal_Class

* make subfolder in logs and temp
foreach folder in externalid Internal mf_unwind {
	cap mkdir $logs/`folder'
	cap mkdir $temp/`folder'
}

* make subfolder in logs, temp, and output
foreach folder in Morningstar_Mapping_Build ER_Data PortfolioSummary HoldingDetail {
	cap mkdir $logs/`folder'
	cap mkdir $temp/`folder'
	cap mkdir $output/`folder'
}

* make some additional folder and subfolders
cap mkdir $temp/mf_unwind/tmp_hd_files
cap mkdir $temp/mf_unwind/mf_scaling_lists
cap mkdir $temp/mf_unwind/hd_period_info

* panelify parameters
global groups = 100

* EMU domiciles
global eu1 `""ITA","DEU","FRA","ESP","GRC","NLD","AUT" "'
global eu2 `""BEL","FIN","PRT","CYP","EST","LAT","LTU","SVK","SVN" "'
global eu3 `""MLT","EMU","LUX","IRL" "'

* excel settings
set excelxlsxlargefile on

* ---------------------------------------
* Launch jobs
* ---------------------------------------

* unwind steps
if substr("`2'",1,6) =="Unwind" {
	do $code_path/morningstar/mf_unwind/`2'.do `5'
}

* externalid steps
else if "`2'"=="externalid_make" {
	do $code_path/morningstar/externalid/externalid_collect_extid_master.do
	do $code_path/morningstar/externalid/externalid_make_extid_csvtodta.do
}
else if substr("`2'",1,10) =="externalid" {
	do $code_path/morningstar/externalid/`2'.do
}

* panelify steps
else if "`2'"=="Reshape_Cut_Groups" {
	do $code_path/morningstar/panelify/Reshape_Cut_Groups.do
}
else if "`2'"=="Split_Annual_Files" {
	do $code_path/morningstar/panelify/Split_Annual_Files.do `5'
}
else if "`2'"=="Reappend_Split_Annuals" {
	do $code_path/morningstar/panelify/Reappend_Split_Annuals.do `5'
}
else if "`2'"=="Panelify_MPID_Splits" {
	do $code_path/morningstar/panelify/Panelify_HD_Splits.do `5'
}
else if "`2'"=="Panelify_MPID_Splits_Covariates" {
	do $code_path/morningstar/panelify/Panelify_HD_Splits_Covariates.do `5'
}
else if "`2'"=="Append_HD_Panels" {
	do $code_path/morningstar/panelify/Append_HD_Panels.do
}
else if "`2'"=="Find_Constant_Panels" {
	do $code_path/morningstar/panelify/Find_Constant_Panels.do
}
else if "`2'"=="Collapse_Panels" {
	do $code_path/morningstar/panelify/Collapse_Panel.do
}

* figi R step 
else if "`2'"=="Figi_API" {
    R CMD BATCH --no-save --no-restore '--args tempdir="$project_path/temp/externalid" rawdir="$project_path/raw/externalid/latest"' $code_path/morningstar/externalid/Figi_API.R $project_path/logs/Figi_API/figi_api.out
}

* all other steps
else {
	do $code_path/morningstar/`2'.do `5'
}

cap log close
